layui.use(['table', 'admin', 'ax', 'func'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var func = layui.func;

    /**
     * 入库单（采购入库、调拨入库）明细管理
     */
    var InStockInfoDetail = {
        tableId: "inStockInfoDetailTable"
    };

    /**
     * 初始化表格的列
     */
    InStockInfoDetail.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: '主键'},
            {field: 'inStockInfoId', sort: false, title: '主表ID'},
            {field: 'orgId', sort: false, title: '门店ID （0表示总部 ）'},
            {field: 'inStockNum', sort: false, title: '入库单号：入库单表 “入库单号“'},
            {field: 'productId', sort: false, title: '商品id'},
            {field: 'productName', sort: false, title: '商品名称'},
            {field: 'producePlace', sort: false, title: '产地'},
            {field: 'brand', sort: false, title: '品牌'},
            {field: 'applyCarModel', sort: false, title: '适用车型'},
            {field: 'supplierId', sort: false, title: '供应商id'},
            {field: 'supplierName', sort: false, title: '供应商名称'},
            {field: 'warehouseId', sort: false, title: '仓库id'},
            {field: 'warehouseName', sort: false, title: '仓库名称'},
            {field: 'shelfId', sort: false, title: '货架id'},
            {field: 'shelfName', sort: false, title: '货架名称'},
            {field: 'count', sort: false, title: '入库数量'},
            {field: 'actualArrivalCount', sort: false, title: '实际到货数量'},
            {field: 'inStockPrice', sort: false, title: '入库单价(单价）'},
            {field: 'inStockTotalAmt', sort: false, title: '入库总金额'},
            {field: 'unit', sort: false, title: '单位'},
            {field: 'inWarehouseType', sort: false, title: '入库方式 1:一般入库(采购入库)  2:调拨入库'},
            {field: 'imagePath', sort: false, title: '图片存储路径'},
            {field: 'position', sort: false, title: '仓位'},
            {field: 'remark', sort: false, title: '备注'},
            {field: 'handleUser', sort: false, title: '经手人id'},
            {field: 'createTime', sort: false, title: '创建时间'},
            {field: 'createUser', sort: false, title: '创建人id'},
            {field: 'auditUser', sort: false, title: '审核人id'},
            {field: 'auditTime', sort: false, title: '审核时间'},
            {field: 'updateTime', sort: false, title: '更新时间'},
            {field: 'printTime', sort: false, title: '打印时间'},
            {field: 'updateUser', sort: false, title: '更新人id'},
            {field: 'isDelete', sort: false, title: '默认为0,1为删除'},
            {field: 'status', sort: false, title: '单据状态'},
            {align: 'center', toolbar: '#tableBar', title: '操作'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    InStockInfoDetail.search = function () {
        var queryData = {};

        queryData['orgId'] = $('#orgId').val();
        queryData['productId'] = $('#productId').val();

        table.reload(InStockInfoDetail.tableId, {
            where: queryData, page: {curr: 1}
        });
    };

    /**
     * 弹出添加对话框
     */
    InStockInfoDetail.openAddDlg = function () {
        func.open({
            title: '添加入库单（采购入库、调拨入库）明细',
            content: Feng.ctxPath + '/inStockInfoDetail/add',
            tableId: InStockInfoDetail.tableId
        });
    };

     /**
      * 点击编辑
      *
      * @param data 点击按钮时候的行数据
      */
      InStockInfoDetail.openEditDlg = function (data) {
          func.open({
              title: '修改入库单（采购入库、调拨入库）明细',
              content: Feng.ctxPath + '/inStockInfoDetail/edit?id=' + data.id,
              tableId: InStockInfoDetail.tableId
          });
      };


    /**
     * 导出excel按钮
     */
    InStockInfoDetail.exportExcel = function () {
        var checkRows = table.checkStatus(InStockInfoDetail.tableId);
        if (checkRows.data.length === 0) {
            Feng.error("请选择要导出的数据");
        } else {
            table.exportFile(tableResult.config.id, checkRows.data, 'xls');
        }
    };

    /**
     * 点击删除
     *
     * @param data 点击按钮时候的行数据
     */
    InStockInfoDetail.onDeleteItem = function (data) {
        var operation = function () {
            var ajax = new $ax(Feng.ctxPath + "/inStockInfoDetail/delete", function (data) {
                Feng.success("删除成功!");
                table.reload(InStockInfoDetail.tableId);
            }, function (data) {
                Feng.error("删除失败!" + data.responseJSON.message + "!");
            });
            ajax.set("id", data.id);
            ajax.start();
        };
        Feng.confirm("是否删除?", operation);
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + InStockInfoDetail.tableId,
        url: Feng.ctxPath + '/inStockInfoDetail/list',
        page: true,
        height: "full-158",
        cellMinWidth: 100,
        cols: InStockInfoDetail.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        InStockInfoDetail.search();
    });

    // 添加按钮点击事件
    $('#btnAdd').click(function () {

    InStockInfoDetail.openAddDlg();

    });

    // 导出excel
    $('#btnExp').click(function () {
        InStockInfoDetail.exportExcel();
    });

    // 工具条点击事件
    table.on('tool(' + InStockInfoDetail.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;

        if (layEvent === 'edit') {
            InStockInfoDetail.openEditDlg(data);
        } else if (layEvent === 'delete') {
            InStockInfoDetail.onDeleteItem(data);
        }
    });
});
